package captcha

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/golang/freetype/truetype"
	"github.com/wenlng/go-captcha-assets/bindata/chars"
	"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
	"github.com/wenlng/go-captcha-assets/resources/images"
	"github.com/wenlng/go-captcha/v2/base/option"
	"github.com/wenlng/go-captcha/v2/click"
	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"
	"product/common/encryption"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateClickCaptchaLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取验证码
func NewCreateClickCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateClickCaptchaLogic {
	return &CreateClickCaptchaLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateClickCaptchaLogic) CreateClickCaptcha(req *types.ClickCaptchaReq) (resp *types.ClickCaptchaResp, err error) {
	captchaB, err := l.init(req)
	if err != nil {
		return nil, err
	}
	captchaData, err := captchaB.Generate()
	if err != nil {
		return nil, err
	}
	checkDotData := captchaData.GetData()
	if checkDotData == nil {
		return nil, errors.New("验证码生成失败")
	}
	var imageBase64, thumbImageBase64 string
	imageBase64, err = captchaData.GetMasterImage().ToBase64()
	if err != nil {
		return nil, err
	}
	thumbImageBase64, err = captchaData.GetThumbImage().ToBase64()
	if thumbImageBase64 == "" {
		return nil, errors.New("缩略图生成失败")
	}
	dotBytes, _ := json.Marshal(checkDotData)
	jsonStr := string(dotBytes)
	fmt.Println("jsonStr", jsonStr)
	// 将数据缓存
	key := encryption.Md5ByBytes(dotBytes)
	_ = l.svcCtx.Redis.SetCtx(l.ctx, key, jsonStr)
	resp = &types.ClickCaptchaResp{
		CaptchaBase64Image:     imageBase64,
		CaptchCheckBase64Image: thumbImageBase64,
		CaptchaKey:             key,
		Ok:                     true,
	}
	return resp, nil
}

// 依据请求参数获取验证类型
func (l *CreateClickCaptchaLogic) init(req *types.ClickCaptchaReq) (click.Captcha, error) {
	builder := click.NewBuilder(
		click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
		click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
		click.WithRangeThumbColors([]string{
			"#1f55c4",
			"#780592",
			"#2f6b00",
			"#910000",
			"#864401",
			"#675901",
			"#016e5c",
		}),
		click.WithRangeColors([]string{
			"#fde98e",
			"#60c1ff",
			"#fcb08e",
			"#fb88ff",
			"#b4fed4",
			"#cbfaa9",
			"#78d6f8",
		}),
		click.WithImageSize(option.Size{Width: 400, Height: 250}))
	fonts, err := fzshengsksjw.GetFont()
	if err != nil {
		return nil, err
	}
	img, err := images.GetImages()
	if err != nil {
		return nil, err
	}
	builder.SetResources(click.WithChars(chars.GetChineseChars()),
		click.WithFonts([]*truetype.Font{fonts}),
		click.WithBackgrounds(img))
	// 默认文字验证码
	if req.ClickCaptchaType == 1 {
		textBuilder := builder.Make()
		return textBuilder, nil
	}

	builder.Clear()
	builder.SetOptions(
		click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
		click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
		click.WithRangeThumbColors([]string{
			"#4a85fb",
			"#d93ffb",
			"#56be01",
			"#ee2b2b",
			"#cd6904",
			"#b49b03",
			"#01ad90",
		}),
		click.WithImageSize(option.Size{Width: 400, Height: 250}))
	builder.SetResources(click.WithChars(chars.GetChineseChars()),
		click.WithFonts([]*truetype.Font{fonts}),
		click.WithBackgrounds(img))
	lightBuilder := builder.Make()
	return lightBuilder, nil
}
